package com.accuweaver.wave;

import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import com.google.wave.api.AbstractRobotServlet;
import com.google.wave.api.Blip;
import com.google.wave.api.Event;
import com.google.wave.api.Gadget;
import com.google.wave.api.GadgetView;
import com.google.wave.api.RobotMessageBundle;
import com.google.wave.api.StyleType;
import com.google.wave.api.StyledText;
import com.google.wave.api.TextView;
import com.google.wave.api.Wavelet;

@SuppressWarnings("serial")
public class WaveridesServlet extends AbstractRobotServlet {
	 private static final String LATITUDY_URL = "http://latitudymap.appspot.com/gadget/latitudymap.xml";
	private static final Logger LOG = Logger.getLogger(WaveridesServlet.class.getName());

	@Override
	public void processEvents(RobotMessageBundle bundle) {
		Wavelet wavelet = bundle.getWavelet(); 
		Blip rootBlip = wavelet.getRootBlip();
		TextView rootTextView = rootBlip.getDocument();
		GadgetView rootGadgetView = rootTextView.getGadgetView();
		Gadget fromLocGadget = new Gadget();
		String fromGadgetName = "from";
		fromLocGadget.setField("name", fromGadgetName);
	    fromLocGadget.setUrl(LATITUDY_URL);
	    
		Gadget toLocGadget = new Gadget();
		String toGadgetName = "to";
		toLocGadget.setField("name", toGadgetName);
	    toLocGadget.setUrl(LATITUDY_URL);

		if (bundle.wasSelfAdded())
		{
			LOG.info("waverider added");
			Blip blip = wavelet.appendBlip();
		    TextView textView = blip.getDocument();
		    textView.append("WaveRides is here to serve your needs ...\n");
		    if (rootGadgetView.getGadget(LATITUDY_URL) == null)
		    {
		    	LOG.info("add latitudy");
		        StyledText fromText = new StyledText("Ride From",
		                StyleType.BOLD);
		        rootTextView.appendStyledText(fromText);
		    	rootGadgetView.append(fromLocGadget);
		        StyledText toText = new StyledText("Ride To",
		                StyleType.BOLD);
		        rootTextView.appendStyledText(toText);
		    	rootGadgetView.append(toLocGadget);
		    } else {
		    	LOG.info("latitudy already added");
		    	textView.append("latitudy already added\n");
		    }
		} else {
			
			List<String> participants = wavelet.getParticipants();
		
		// Now monitor events ...
		for (Event e: bundle.getEvents()) {
			
			switch (e.getType()) {
			case WAVELET_PARTICIPANTS_CHANGED:
		    	LOG.info("participant changed");
		    	for (String participant : participants){
		    		LOG.info("Participant: " + participant);
		    	}
		    	break;

			case FORM_BUTTON_CLICKED:
				LOG.info("form button clicked");
				break;
			
			case DOCUMENT_CHANGED:
		    	LOG.info("document changed");
		    	
				
				// Add a new blip to the wave - FIXME add logic to delete old status blip ...
		        Blip blip = wavelet.appendBlip();
		        TextView textView = blip.getDocument();
		        textView.append("WaveRides is managing the car pool search ...\n");
		        
		        LOG.info("getting reference to latitudy");
		        textView.append("Getting reference to latitudy ...\n");

		        // Get the gadgetView from the root blip ...
		        rootGadgetView = rootBlip.getDocument().getGadgetView();
		        
		        List<Gadget> gadgets = rootGadgetView.getGadgets();
		        boolean hasData = false;
		        
		        LOG.info("Found " + gadgets.size() + " gadgets");
		        for (Gadget gadget : gadgets)
		        {
		        	if (gadget.getUrl().equalsIgnoreCase(LATITUDY_URL)){
		        		
		        		if (gadget.getField("name").equalsIgnoreCase(fromGadgetName)) {
		    		        LOG.info("getting reference to fromGadget");
//		    		        textView.append("Getting reference to fromGadget ...\n");
		        			fromLocGadget = gadget;
		        			hasData = true;
		        		}
		        		if (gadget.getField("name").equalsIgnoreCase(toGadgetName)) {
		    		        LOG.info("getting reference to toGadget");
//		    		        textView.append("Getting reference to toGadget ...\n");
		        			toLocGadget = gadget;
		        			hasData = true;
		        		}
		        	}
		        }
		        if (hasData){
//		        	displayGadgetProperties(fromLocGadget, textView);
//		        	displayGadgetProperties(toLocGadget, textView);
		        	textView.append("\n----- From ------\n\n");
		        	displayLocation(fromLocGadget, participants, textView);
		        	textView.append("\n----- To -------\n\n");
		        	displayLocation(toLocGadget, participants, textView);
		        }
		      }
		    }
		}

	}



	private void displayLocation(Gadget fromLocGadget,
			List<String> participants, TextView textView) {
		for(String participant : participants){
			if (!participant.endsWith("appspot.com")) {
				Map<String, Object> fromProperties = fromLocGadget.getProperties();
				for (String key : fromProperties.keySet()){
					if ((key.contains("_place")) && (key.contains(participant))){
						LOG.info("Participant '" + participant + "' - from = " + fromProperties.get(key));
						textView.append("Participant '" + participant + "' = " + fromProperties.get(key) + "\n");
					}
				}
				
			}
			
		}
	}
	

	
/*	private void displayGadgetProperties(Gadget thisGadget, TextView thisTextView) {
		if (thisGadget != null){
			Map<String,Object> properties = thisGadget.getProperties();
			LOG.info("Found " + properties.size() + "properties ...");
			thisTextView.append("Found " + properties.size() + " properties ...\n\n");
		    for (String propertyName : properties.keySet())
		    {
		    	// Loop through properties ....
		    	LOG.info(propertyName + " -- " + properties.get(propertyName));
		    	thisTextView.append(propertyName + " -- " + properties.get(propertyName) + "\n");
		    }
		}
	}
	*/

}
